import {selectOrder,toChangePrice,cancelOrder} from "../../ajax/module/userInfo.js" 
import {selectOption} from "./selectOption.js"
import imgUrl from "../../images/util.js"
let app=getApp()
Page({
    data:{
        title:"订单列表",
        titleFlg:true,
        isLoad:true,
        isHave:true,
        isPricePop:false,
        isCancelOrder:false,
        CustomBar:60,
        TabCur: 0,
        logo:imgUrl.logoBase64,
        orderIcon:imgUrl.orderIcon,
        duigouIcon:imgUrl.duigouIcon,
        tabList:[
            {
                name:"全部",
                id:"0",
                status:""
            },
            {
                name:"待付款",
                id:"1",
                status:"300"
            },
            {
                name:"待服务(未关联家政师)",
                id:"2",
                status:"400"
            },
            {
                name:"待服务",
                id:"3",
                status:"500"
            },
            {
                name:"已完成",
                id:"4",
                status:"700"
            },
            {
                name:"已关闭",
                id:"5",
                status:"800"
            }
        ],
        sanjiaoType:true,  //排序
        phone:"",
        status:"",
        pageNum:"0",
        pageSize:"10",
        list:[
        //     {
        //     itemList:[{
        //         goodsAmount:1,
        //         price:200,
        //         categoryName:"家庭保洁4小时"
        //     },{
        //         goodsAmount:2,
        //         price:2000,
        //         categoryName:"家庭保洁4小时"
        //     }]
        // }
        ],
        afterPrice:"",
        currentSelectOrderPrice:"",
        currentSelectOrderId:"",
        reasonArr:[
            {
                name:" 无法联系到买家",
                id:1,
                select:false
            },
            {
                name:"买家误拍了或重拍了",
                id:2,
                select:false
            },
            {
                name:" 买家无诚意完成交易",
                id:3,
                select:false
            },
            {
                name:"已通过银行线下汇款",
                id:4,
                select:false
            },
            {
                name:" 已通过同城见面交易",
                id:5,
                select:false
            },
            {
                name:"已通过货到 付款交易",
                id:6,
                select:false
            },
            {
                name:" 已通过网上银行直接汇款",
                id:7,
                select:false
            },
            {
                name:"已经缺货无法交易",
                id:8,
                select:false
            }
        ],
        selectReason:""
    },
    onPullDownRefresh() {
        this.setData({
            title:"加载中",
            titleFlg:false
        })
        wx.showNavigationBarLoading()
       
        setTimeout(()=>
        {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
         
          this.setData({
            title:"订单列表",
            titleFlg:true,
            list:[],
            pageNum:0+""
           })
           this.select()
        },3000);
    },
    tabSelect(e) {
        this.setData({
          TabCur: e.currentTarget.dataset.item.id,
          status: e.currentTarget.dataset.item.status,
          pageNum:0+"",
          list:[]   
        })

        this.select()
    },
    // 排序
    timeSort(){
       this.setData({
        list:[],
        pageNum:0+"",
        sanjiaoType:!this.data.sanjiaoType
       })
       this.select()
    },
    bindinputPhone(e){
        this.setData({
            phone:e.detail.value+""
        })
    },
    onReachBottom(){
        this.setData({
            isLoad:false,
           
        })
        this.select()
        setTimeout(()=>{
            this.setData({
                isLoad:true
            })
        },2000)
    },
    onLoad(){
        // this.onPullDownRefresh()
    },
    onShow(){
        // wx.hideTabBar({
        //   animation: true,
        // })
        this.setData({
            pageNum:0,
            list:[]
        })
       this.select()
    },
    selectByPhone(){
       this.setData({
           pageNum:0,
           list:[]
       })
       this.select()
    },
    selectReason(e){
        let id=e.currentTarget.dataset.item.id
        let tempArr=JSON.parse(JSON.stringify(this.data.reasonArr))
        tempArr.forEach(item=>{
            item.select=false
            if(item.id == id){
                item.select=true
                this.setData({
                    selectReason:item.name
                })
            }
        })
        this.setData({
            reasonArr:tempArr
        })
    },
    contactBuyer(e){
        // console.log(e.currentTarget.dataset.buyer.contactPhone)
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.buyer.contactPhone,
            success: function () {
              console.log('成功拨打电话')
            }
          })
    },
    changePrice(e){
        // console.log(e.currentTarget.dataset.item.actualMoney)
        wx.hideTabBar({
          animation: true,
        })
        this.setData({
            isPricePop:true,
            currentSelectOrderPrice:e.currentTarget.dataset.item.actualMoney,
            currentSelectOrderId:e.currentTarget.dataset.item.orderId
        })
    },
    sureChange(){
        let orderId=this.data.currentSelectOrderId
        let price=this.data.afterPrice
        // "orderId": 0,
        // "price": 0
        if((this.data.afterPrice+"") !==""){
            toChangePrice({orderId,price}).then(res=>{
                wx.showToast({
                    title: '修改价格成功',
                    duration:2000
                })

                this.setData({
                    isPricePop:false
                })
                wx.showTabBar({
                  animation: true,
                })
                this.onShow()
            })
        }else{
            wx.showToast({
              title: '请输入修改价格',
              duration:2000
            })
        }
       
    },
    sureCancel(){
        if(this.data.selectReason!==""){
            let cancelDesc=this.data.selectReason
            let orderId=this.data.currentSelectOrderId
            console.log("确认取消")
    
            cancelOrder({cancelDesc,orderId}).then(res=>{
                 wx.showToast({
                   title: '取消订单成功',
                 })
                 wx.showTabBar({
                    animation: true,
                 })
                 this.setData({
                     isCancelOrder:false
                 })
                //  this.select()
                this.onShow()
            })
        }else{
            wx.showToast({
                title: '请选择理由',
                icon:"none"
              })
        }
       
    },
    cancelOrder(e){
        console.log(e)
        wx.hideTabBar({
          animation: true,
        })
        this.setData({
            currentSelectOrderId:e.currentTarget.dataset.item.orderId,
            isCancelOrder:true
        })
    },
    hideModal(){
        wx.showTabBar({
          animation: true,
        })
        this.setData({
            isPricePop:false,
            isCancelOrder:false
        })
    },
    inputPrice(e){
         this.setData({
             afterPrice:e.detail.value+""
         })
    },
    jumpAppointment(e){
        console.log("qq", e.currentTarget.dataset.item)
        app.globalData.orderUser=e.currentTarget.dataset.item.userContactAddress
        app.globalData.orderSku=e.currentTarget.dataset.item.itemList
        app.globalData.other={
            orderId:e.currentTarget.dataset.item.orderId,
            isAssign:e.currentTarget.dataset.item.isAssign
        }
        wx.navigateTo({
          url: '../appointment/appointment',
        })
    },
    changeAppointment(e){
        // console.log(e)
        app.globalData.orderUser=e.currentTarget.dataset.item.userContactAddress
        app.globalData.orderSku=e.currentTarget.dataset.item.itemList
        app.globalData.other={
            orderId:e.currentTarget.dataset.item.orderId,
            isAssign:e.currentTarget.dataset.item.isAssign
        }
        app.globalData.appointEmp={
            employeeName:e.currentTarget.dataset.item.employee.employeeName||"",
            employeePhone:e.currentTarget.dataset.item.employee.employeePhone||"",
            employeeId:e.currentTarget.dataset.item.employee.employeeId,
        }
        // 计算时长
        let temp1=  new Date((e.currentTarget.dataset.item.startTime+"").replace(/-/g, '/')).getTime()
        let temp2=  new Date((e.currentTarget.dataset.item.endTime+"").replace(/-/g, '/')).getTime()

        // console.log(( (Number(temp2) - Number(temp1))/1000/60/60).toFixed(2) ) 

        app.globalData.appointmentTime={
            text:e.currentTarget.dataset.item.startTime+"至"+e.currentTarget.dataset.item.endTime.split(" ")[1],
            timeLength:((Number(temp2) - Number(temp1))/1000/60/60).toFixed(2) +"小时"
        }


        // appointmentTime:app.globalData.appointmentTime,
    //   appointEmp:app.globalData.appointEmp,
        // employeeName   employeePhone
        // text   timeLength  startTime


        wx.navigateTo({
            url: '../submitAppointment/submitAppointment?id=1',
          })
    },
    select(){
        /**
         * status 300 待付款   400待服务 700 已完成 800 已关闭
         */
        this.setData({
            pageNum:Number(this.data.pageNum)+1 +""
        })
        let selectMap=new Map()
        selectMap.set("phone",this.data.phone)
        selectMap.set("status",this.data.status)
        selectMap.set("pageNum",this.data.pageNum)
        selectMap.set("pageSize",this.data.pageSize)
        
        new selectOption(selectMap).then(option=>{
          console.log(option)

          selectOrder(option).then(res=>{
            let newArr=this.data.sanjiaoType?res.data.rows:res.data.rows.reverse()
            let finalArr=this.data.list.concat(newArr)
            this.setData({
                list:finalArr
            })
            if(finalArr.length == 0){
                this.setData({
                    isHave:true
                })
            }else{
                this.setData({
                    isHave:false
                })
            }
          })
        })
       
    }

    // orderCode 订单号
    // createTime 创建时间
    // userContactAddress 买家地址
    // actualMoney 实付款
    // orderStatusName 状态
    // orderTypeName  实物订单


//  sku    itemList []

//     categoryName: "家庭保洁4小时"
// goodsAmount: 1
// goodsImage: "http://tiantue-image.oss-cn-shenzhen.aliyuncs.com/images/202101/141610609425664.jpg"
// goodsName: "家庭保洁4小时"
// goodsServiceId: null
// parentCategoryName: "家庭保洁4小时"
// price: 200
// remark: null
})